<?php
/**
 * licenses : MIT
 * creatTime: 2020/5/15
 * author : weblinuxgame
 * filename: UserRegister.php
 */

namespace App\Api\Requests\User;

use Illuminate\Http\Request;
use OpenApi\Annotations as OA;
use App\Core\Traits\Api\RequestParamsTrait;
use App\Core\Interfaces\RequestParamsInterface;


/**
 * @OA\Schema(
 *     schema="UserRegister",
 *     title="UserRegister",
 *     description="用户注册基本数据"
 *  )
 *
 * 用户注册数据
 * Class UserRegister
 * @package App\Api\Requests\User
 */
class UserRegister implements RequestParamsInterface
{
    use RequestParamsTrait;

    /**
     * @OA\Property(
     *     title="username",
     *     format="string",
     *     description="用户名|账号",
     *     required={"username"},
     * )
     * @var string
     */
    public $username;


    /**
     * @OA\Property(
     *     title="email",
     *     format="string",
     *     description="注册邮箱",
     *     required={"email"},
     * )
     * @var string
     */
    public $email;


    /**
     * @OA\Property(
     *     title="mobile",
     *     format="string",
     *     description="注册手机号",
     *     required={"mobile"},
     * )
     * @var string
     */
    public $mobile;


    /**
     * @OA\Property(
     *     title="password",
     *     format="string",
     *     description="密码,传输过程中使用base64",
     *     required={"password"},
     * )
     * @var string
     */
    public $password;


    /**
     * @OA\Property(
     *     title="ensurePassword",
     *     format="string",
     *     description="确认密码,传输过程中使用base64,以防用户填写错误",
     *     required={"ensurePassword"},
     * )
     * @var string
     */
    public $ensurePassword;


    /**
     * @OA\Property(
     *     title="gender",
     *     format="int",
     *     description="用户性别,0:无,1:男,2:女,3:未知",
     *     default="1"
     * )
     * @var int
     */
    public $gender;

    /**
     * @OA\Property(
     *     title="captcha",
     *     format="string",
     *     description="注册图形验证码",
     *     default="123456"
     * )
     * @var string
     */
    public $captcha;

    // 验证规则
    protected $_rules = [];

    protected $_array = [];

    protected $_validator;


    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->initialize($this->request);
        $this->initializeAfter();
    }

    /**
     * 其他初始化
     */
    protected function initializeAfter()
    {
        $this->getVars();
        if (!empty($this->password) && !empty($this->ensurePassword)) {
            $this->_array['password'] = base64_decode($this->password);
            $this->password = $this->_array['password'];
            $this->_array['ensurePassword'] = base64_decode($this->ensurePassword);
            $this->ensurePassword = $this->_array['ensurePassword'];
        }
        $this->_rules = [
            'email' => 'bail|required|email|unique:users',
            'gender' => 'alpha_num|in:0,1,2',
            'password' => 'required|min:6|max:128',
            'ensurePassword' => 'required|same:password',
            'mobile' => 'bail|required|unique:users',
            'captcha' => ['alpha_dash', 'min:4', 'max:6', 'captcha_check'],
            'username' => 'bail|required|unique:users|max:100|min:2',
        ];
    }

    public function toArray()
    {
        return $this->getVars();
    }

    public function check(): bool
    {
        return $this->verify();
    }
}
